﻿
Imports System.IO
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports wfileis.data

Public Class writer
    Inherits container

    Public Const resultIFrameCallback As String = "wrt_cb"
#If RETIRED Then
    Public Const resultIFrameCallback2 As String = "wrt_cb2"
#End If
    Public Const timeoutVar As String = "wrt_to"
    Public Const timeout As Int64 = 10
    Private Shared writercss As String = Nothing

    Public Class datasource
        Public page As xhtmlpage = Nothing
        Public context As httpContext = Nothing
        Public key As String = Nothing
        Public info As info = Nothing
        Public category As String = Nothing
        Public toviewpage As Boolean = False
        Public posterDS As poster.datasource = Nothing

        Private Sub initial(ByVal page As xhtmlpage, ByVal context As httpContext, _
                            ByVal key As String, ByVal info As info, ByVal category As String, _
                            ByVal toviewpage As Boolean)
            'these two are shared in context
            Me.page = page
            Me.context = context
            'copy to make it safe
            copy(Me.key, key)
            copy(Me.info, info)
            copy(Me.category, category)
            Me.toviewpage = toviewpage
            assert(Not context Is Nothing, "context is nothing.")
            posterDS = New poster.datasource()
            posterDS.inputName = query.text
            posterDS.page = page
            posterDS.previewUrl = strmid(posterPreview._pathidentity, strlen(network.pathSeperator))
            posterDS.previewText = context.string(stringId.POSTER_PREVIEWTEXT)
            posterDS.imageName = context.string(stringId.POSTER_IMAGENAME)
            posterDS.imageText = context.string(stringId.POSTER_IMAGETEXT)
            posterDS.imageInputTitle = context.string(stringId.POSTER_IMAGEINPUTTITLE)
            posterDS.urlName = context.string(stringId.POSTER_URLNAME)
            posterDS.urlText = context.string(stringId.POSTER_URLTEXT)
            posterDS.urlInputTitle = context.string(stringId.POSTER_URLINPUTTITLE)
            posterDS.mediaName = context.string(stringId.POSTER_MEDIANAME)
            posterDS.mediaText = context.string(stringId.POSTER_MEDIATEXT)
            posterDS.mediaInputTitle = context.string(stringId.POSTER_MEDIAINPUTTITLE)
            posterDS.flashName = context.string(stringId.POSTER_FLASHNAME)
            posterDS.flashText = context.string(stringId.POSTER_FLASHTEXT)
            posterDS.flashInputTitle = context.string(stringId.POSTER_FLASHINPUTTITLE)
            posterDS.locationName = context.string(stringId.POSTER_LOCATIONNAME)
            posterDS.locationText = context.string(stringId.POSTER_LOCATIONTEXT)
            posterDS.locationInputTitle = context.string(stringId.POSTER_LOCATIONINPUTTITLE)
            posterDS.supportedTagsText = context.string(stringId.POSTER_SUPPORTEDTAGS)
        End Sub

        Public Sub New(ByVal page As xhtmlpage, ByVal context As httpContext, _
                       ByVal key As String, ByVal info As info, ByVal category As String, _
                       ByVal toviewpage As Boolean)
            initial(page, context, key, info, category, toviewpage)
        End Sub

        Public Sub New(ByVal page As xhtmlpage, ByVal context As httpContext, _
                       ByVal toviewpage As Boolean)
            assert(Not context Is Nothing, "context is nothing.")
            initial(page, context, context.url().query(query.key), _
                    info.generate(context), context.url().query(query.category), toviewpage)
        End Sub
    End Class

    Public Sub setData(ByVal ds As datasource, Optional ByRef cs As String = Nothing)
        Const formId As String = "wrt_fm"
        Const resultIFrameName As String = "wrt_rst"
        Const keyinputId As String = "wrt_key"

        If isdebugmode() Then
            'read css in each request
            readcss()
        End If
        assert(Not ds Is Nothing, "should not new with an empty writerDS.")
        assert(Not ds.page Is Nothing, "should not new with an empty writerDS.xhtmlpage.")
        assert(Not ds.context Is Nothing, "should not new with an empty httpContext.")

        ds.page.inlineStyle().addstyle("wfileis_writer_css", writercss)

        Dim context As httpContext = Nothing
        context = ds.context

        Dim container As div = Nothing
        container = context.createControl(New div())
        container.class = "wrt"
        addchild(container)

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.action = strmid(submit._pathidentity, strlen(network.pathSeperator))
        form.method = xhtmlControl_1_1.method_enum.post
        form.id = formId
        If Not ds.toviewpage Then
            form.target = resultIFrameName
        End If

        Dim div As div = Nothing
        div = context.createControl(New div())
        form.addchild(div)

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.WRITER_KEY)
        label.class = "lb"
        div.addchild(label)

        Dim inputwrap As span = Nothing
        inputwrap = context.createControl(New span())
        inputwrap.class = "iw ln"
        div.addchild(inputwrap)

        Dim antiInputSpam As antiInputSpam = Nothing
        antiInputSpam = context.createControl(New antiInputSpam(ds.page, query.checksum, cs))
        form.addchild(antiInputSpam)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.key

        If Not isemptystring(ds.key) Then
            input.value = ds.key
        End If
        input.id = keyinputId
        input.class = "t"
        inputwrap.addchild(input)

        div = context.createControl(New div())
        form.addchild(div)

        label = context.createControl(New label())
        label.text = context.string(stringId.WRITER_ID)
        label.class = "lb"
        div.addchild(label)

        inputwrap = context.createControl(New span())
        inputwrap.class = "iw sh"
        div.addchild(inputwrap)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.id
        If Not ds.info Is Nothing AndAlso Not isemptystring(ds.info.id) Then
            input.value = ds.info.id
        End If
        input.class = "t"
        inputwrap.addchild(input)

        label = context.createControl(New label())
        label.text = context.string(stringId.WRITER_CATEGORY)
        label.class = "lb"
        div.addchild(label)

        inputwrap = context.createControl(New span())
        inputwrap.class = "iw ex"
        div.addchild(inputwrap)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.category
        If Not isemptystring(ds.category) Then
            input.value = ds.category
        End If
        input.class = "t"
        inputwrap.addchild(input)

        label = context.createControl(New label())
        label.text = context.string(stringId.WRITER_CATMORE)
        div.addchild(label)

        div = context.createControl(New div())
        form.addchild(div)

        Dim inputId As String = Nothing
        Dim poster As poster = Nothing
        poster = context.createControl(New poster())
        poster.setData(ds.posterDS, inputId)
        div.addchild(poster)

        Const submitCall As String = "wrt_sbt"
        Dim submitAction As String = Nothing
        submitAction = onsubmitById.invoke(ds.page.inlineScript(), "'" + formId + "'") + ";" +
                       submitById.invoke(ds.page.inlineScript(), "'" + formId + "'") + ";"

        ds.page.backgroundDownloader().add("""" + staticrequest.generateStaticfileLinkUrl("wi.jpg") + """")
        If Not ds.toviewpage Then
            Dim iframe As iframe2 = Nothing
            iframe = context.createControl(New iframe2())
            iframe.id = resultIFrameName
            iframe.name = resultIFrameName
            ds.page.body().onload += displayById.invoke(ds.page.inlineScript(), _
                                                        "'" + resultIFrameName + "'", "'none'") + ";"
#If RETIRED Then
            ds.page.inlineScript().addscript("wrt_rst_callback", _
                                             "function " + resultIFrameCallback + "(){" + _
                                                 displayById.invoke(ds.page.inlineScript(), _
                                                                    "'" + resultIFrameName + "'", _
                                                                    "'none'") + ";" + _
                                                 ifelse.invoke( _
                                                     existfunction.invoke(ds.page, "'" + resultIFrameCallback2 + "'"), _
                                                     resultIFrameCallback2 + "(" + _
                                                     encodeURIComponent.invoke(ds.page.inlineScript(), _
                                                                               getValueById.invoke( _
                                                                                   ds.page.inlineScript(), _
                                                                                   "'" + keyinputId + "'") _
                                                     ) + ")") + _
                                                 clearTimeout.invoke(ds.page.inlineScript(), timeoutVar) + ";" + _
                                             "}")
            ds.page.inlineScript().addscript("wrt_rst_callback2", _
                                             "function " + resultIFrameCallback2 + "(k){" + _
                                             view.generateLastViewlink("'+k+'") + ";}")
#End If
            ds.page.inlineScript().addscript("wrt_rst_callback", _
                                             "function " + resultIFrameCallback + "(g){" + _
                                                 ifelse.invoke("g",
                                                     changeInnerHtmlById.invoke(ds.page.inlineScript(), _
                                                                                "'" + inputId + "'", _
                                                                                "''") + ";" + _
                                                     view.generateLastViewlink("'+" + _
                                                         encodeURIComponent.invoke( _
                                                             ds.page.inlineScript(), _
                                                             getValueById.invoke( _
                                                                 ds.page.inlineScript(), _
                                                                 "'" + keyinputId + "'")) + "+'")) + _
                                                 displayById.invoke(ds.page.inlineScript(), _
                                                                    "'" + resultIFrameName + "'", _
                                                                    "'none'") + ";" + _
                                                 clearTimeout.invoke(ds.page.inlineScript(), timeoutVar) + ";" + _
                                             "}")
            ds.page.inlineScript().addscript("writer_timeoutVar", webctrl.var.invoke(timeoutVar) + ";")
            iframe.attributes("allowtransparency") = "true"
            iframe.scrolling = xhtmlControl_1_1.scrolling_enum.no
            container.addchild(iframe)
            submitAction += displayById.invoke(ds.page.inlineScript(), _
                                               "'" + resultIFrameName + "'", "'block'") + ";" + _
                            timeoutVar + "=" + setTimeout.invoke(ds.page, "'" + _
                                                                 resultIFrameCallback + "(" + _
                                                                     webctrl.[false].name(ds.page.inlineScript()) + _
                                                                 ")'", _
                                                                 secondsToMilliseconds(timeout << 1)) + ";"
        Else
            input = context.createControl(New input())
            input.type = xhtmlctrl.input.type_enum.hidden
            input.name = query.toviewpage
            input.value = "t"
            form.addchild(input)
        End If

        ds.page.inlineScript().addscript("writer.submit", _
                                         "function " + submitCall + "(){" + submitAction + "}")

        Const nokeyConfirmTextVar As String = "wrt_nkt"
        webctrl.var.invoke(ds.page.inlineScript(), nokeyConfirmTextVar, _
                           createString.invoke(context.string(stringId.WRITER_NOKEYCONFIRM)))

        Const submitOnClickAction As String = "wrt_st"
        ds.page.inlineScript().addscript("writer.submitOnClickAction", _
                                         "function " + submitOnClickAction + "(){" + _
                                             ifelse.invoke( _
                                                 getValueById.invoke(ds.page.inlineScript(), _
                                                                     "'" + keyinputId + "'") + _
                                                 "==''", _
                                                 confirm2.invoke(ds.page.inlineScript(), _
                                                                 nokeyConfirmTextVar, _
                                                                 "function(){" + _
                                                                 changeValueById.invoke(ds.page.inlineScript(), _
                                                                     "'" + keyinputId + "'", _
                                                                     localenow.invoke(ds.page.inlineScript())) + ";" + _
                                                                     submitCall + "();}"), _
                                                 submitCall + "()") + ";}")
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.button
        input.value = context.string(stringId.WRITER_SUBMIT)
        input.class = "s"
        input.onclick = submitOnClickAction + "()"
        form.addchild(input)

        'put the form at the end, since iframe needs to put before it
        container.addchild(form)
    End Sub

    Public Sub New(ByVal ds As datasource, _
                   Optional ByVal buff As Stream = Nothing, _
                   Optional ByRef cs As String = Nothing)
        Me.New(buff)
        setData(ds, cs)
    End Sub

    Public Sub New(ByVal ds As datasource, _
                   ByRef cs As String, _
                   Optional ByRef buff As Stream = Nothing)
        Me.New(ds, buff, cs)
    End Sub

    Public Sub New(Optional ByVal buff As MemoryStream = Nothing)
        MyBase.New(buff)
    End Sub

    Private Shared Sub readcss()
        writercss = cssrequest.readcss("writer.css")
    End Sub

    Shared Sub New()
        readcss()
    End Sub
End Class
